/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.drawing;

import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.List;

import twaver.TUIManager;
import twaver.TWaverUtil;
import twaver.network.InteractionMode;
import twaver.network.TNetwork;
import twaver.network.inputhandler.CreateResizableNodeInputHandler;
import twaver.network.inputhandler.CreateShapeNodeInputHandler;
import twaver.network.inputhandler.DefaultInputHandler;
import twaver.network.inputhandler.InputHandler;
import twaver.network.toolbar.BaseNetworkDnDButton;
import twaver.network.toolbar.BaseNetworkToggleButton;
import demo.editor.drawing.element.CubicCurve;
import demo.editor.drawing.element.Line;
import demo.editor.drawing.element.Polygon;
import demo.editor.drawing.element.Polyline;
import demo.editor.drawing.element.QuadCurve;
import demo.editor.drawing.element.Rectangle;
import demo.editor.drawing.element.Round;
import demo.editor.drawing.element.Star;
import demo.editor.drawing.element.Text;
import demo.editor.drawing.element.Triangle;

public abstract class DrawingButton extends BaseNetworkToggleButton{
	
	public static class TextButton extends BaseNetworkDnDButton {			
	    public TextButton(TNetwork network) {
	        super(network, Text.class);
	        this.setIcon(TUIManager.getIcon(Text.class));
	        this.setToolTipText(TWaverUtil.getClassNameWithoutPackage(Text.class));
	    }
	}
	
	public static class RectangleButton extends DrawingButton{
		public RectangleButton(TNetwork network) {
			super(Rectangle.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateResizableNodeInputHandler(network, Rectangle.class)
	        };
		}
	}
	
	public static class RoundButton extends DrawingButton{
		public RoundButton(TNetwork network) {
			super(Round.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateResizableNodeInputHandler(network, Round.class){
	        			public void mark(Graphics2D g) {
	        				TWaverUtil.drawDashRound(
	        						g, this.getMousePressedPoint(), 
	        						this.getMouseCurrentPoint(), 
	        						network.getZoomer().getZoom());
	        			}
	        		}
	        };
		}
	}
	
	public static class TriangleButton extends DrawingButton{
		public TriangleButton(TNetwork network) {
			super(Triangle.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateResizableNodeInputHandler(network, Triangle.class)
	        };
		}
	}
	
	public static class StarButton extends DrawingButton{
		public StarButton(TNetwork network) {
			super(Star.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateResizableNodeInputHandler(network, Star.class)
	        };
		}
	}
	
	public static class LineButton extends DrawingButton{
		public LineButton(TNetwork network) {
			super(Line.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateShapeNodeInputHandler(network, Line.class){
	        			public boolean multiPointProcess(MouseEvent e, List points, List buttonTags) {
	        				if(points.size() != 2){
	        					return false;
	        				}
	        				return super.multiPointProcess(e, points, buttonTags);
	        			}
	        		}
	        };
		}
	}
	
	public static class PolylineButton extends DrawingButton{
		public PolylineButton(TNetwork network) {
			super(Polyline.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateShapeNodeInputHandler(network, Polyline.class){
	        			public boolean multiPointProcess(MouseEvent e, List points, List buttonTags) {
	        				if(points.size() < 3 || e.getClickCount() < 2){
	        					return false;
	        				}
	        				return super.multiPointProcess(e, points, buttonTags);
	        			}
	        		}
	        };
		}
	}
	
	public static class PolygonButton extends DrawingButton{
		public PolygonButton(TNetwork network) {
			super(Polygon.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateShapeNodeInputHandler(network, Polygon.class){
	        			public boolean multiPointProcess(MouseEvent e, List points, List buttonTags) {
	        				if(points.size() < 3 || e.getClickCount() < 2){
	        					return false;
	        				}
	        				return super.multiPointProcess(e, points, buttonTags);
	        			}
	        		}
	        };
		}
	}
	
	public static class CubicCurveButton extends DrawingButton{
		public CubicCurveButton(TNetwork network) {
			super(CubicCurve.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateShapeNodeInputHandler(network, CubicCurve.class){
	        			public boolean multiPointProcess(MouseEvent e, List points, List buttonTags) {
	        				if(points.size() < 3 || e.getClickCount() < 2){
	        					return false;
	        				}
	        				return super.multiPointProcess(e, points, buttonTags);
	        			}
	        		}
	        };
		}
	}
	
	public static class QuadCurveButton extends DrawingButton{
		public QuadCurveButton(TNetwork network) {
			super(QuadCurve.class, network);
		}
		protected InputHandler[] getInputHandler(){
			return new InputHandler[]{ 
	        		new DefaultInputHandler(network), 
	        		new CreateShapeNodeInputHandler(network, QuadCurve.class){
	        			public boolean multiPointProcess(MouseEvent e, List points, List buttonTags) {
	        				if(points.size() < 3 || e.getClickCount() < 2){
	        					return false;
	        				}
	        				return super.multiPointProcess(e, points, buttonTags);
	        			}
	        		}
	        };
		}
	}
	protected abstract InputHandler[] getInputHandler();
	
	protected Class equipmentType = null;
	
	public DrawingButton(final Class equipmentType, final TNetwork network){
		super(network);
		this.equipmentType = equipmentType;
		this.setToolTipText(TWaverUtil.getClassNameWithoutPackage(equipmentType));
		
        this.setDefaultButtonGroup();
	    this.setIcon(TUIManager.getIcon(equipmentType));
		
		this.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				network.setInteractionMode(new InteractionMode(getInputHandler()));
			}
		});
		
	}
	
}